package com.dinian.demeter.service.impl;

import com.dinian.demeter.domain.User;
import com.dinian.demeter.mapper.UserMapper;
import com.dinian.demeter.service.UserService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    @Override
    public User findUserByUsername(String username) {
        User user = userMapper.queryUserByUsername(username);
        return user;
    }

    /**
     * 根据用户id查询用户
     * @param userId
     * @return
     */
    @Override
    public User findUserById(Long userId) {
        User user = userMapper.queryUserById(userId);
        return user;
    }

    /**
     * 修改密码
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @Override
    public Boolean updatePwd(String username,String oldPwd, String newPwd) {
        //查询密码by username
        User user = userMapper.queryUserByUsername(username);
        if(user.getPassword().equals(oldPwd)){
            user.setPassword(newPwd);
            userMapper.updateUser(user);
            log.info("password modify success: {}",newPwd);
            return true;
        }
        return false;
    }
}
